<?php

class EsqueciminhasenhaController extends Site_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->view->submenu_page = TRUE;
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/esqueciminhasenha.js');
        
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
           
            $email = $data['cad_email'];
        
            if($email != FALSE && strlen($email) > 1){

                $obj = new Ead_Model_DbTable_Usuarios();
                $result = $obj->fetchRow('USU_EMAIL like "'.$email.'"');

                if(isset($result->USU_EMAIL)){
                    
                    $senha = date('YmdHis');
                    
                    $data = array('USU_SENHA' => sha1($senha));
                    
                    $obj->update($data, 'USU_ID = ' . $result['USU_ID']);
                    
                    $arr['email'] = $result['USU_EMAIL'];
                    $arr['nome'] = $result['USU_NOME'];
                    $arr['login'] = $result['USU_LOGIN'];
                    $arr['senha'] = $senha;
                    $arr['urlbase'] = $this->_urlbase;
                    $arr['usu_id'] = $id;
                    Zend_Loader::loadClass('envioemail','library');
                    envioemail::senhaCadastro($arr);
                    
                    $this->_helper->redirector('sucesso');
                }
            }
        }
         
    }
    public function sucessoAction(){
        
    }
    public function valemailAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        
        $email = $this->_getParam('email', FALSE);
        
        if($email != FALSE && strlen($email) > 1){
            
            $obj = new Ead_Model_DbTable_Usuarios();
            $result = $obj->fetchRow('USU_EMAIL like "'.$email.'"');

            if(isset($result->USU_EMAIL)){
                echo json_encode(array('ok' => TRUE)); 
            }
            else{
                echo json_encode(array('ok' => FALSE,'msg'=>'E-mail não existe!'));                
            }
        }
        
    }


}

